package com.biglead.admin.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biglead.admin.mapper.UmsRoleDao;
import com.biglead.admin.mapper.UmsRoleMapper;
import com.biglead.admin.mapper.UmsRoleMenuRelationMapper;
import com.biglead.admin.mapper.UmsRoleResourceRelationMapper;
import com.biglead.admin.pojo.*;
import com.biglead.admin.service.UmsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 后台角色管理Service实现类
 * Created by macro on 2018/9/30.
 */
@Service("roleService")
public class UmsRoleServiceImpl extends ServiceImpl<UmsRoleMapper, UmsRole> implements UmsRoleService {
    @Autowired
    private UmsRoleDao umsRoleDao;

    @Autowired
    private UmsRoleMenuRelationMapper umsRoleMenuRelationMapper;
    @Autowired
    private UmsRoleResourceRelationMapper umsRoleResourceRelationMapper;

    /**
     * 根据管理员ID获取对应菜单
     *
     * @param adminId
     */
    @Override
    public List<UmsMenu> getMenuList(Long adminId) {
        return umsRoleDao.getMenuList(adminId);
    }

    @Override
    public boolean create(UmsRole role) {
        role.setCreateTime(new Date());
        role.setAdminCount(0);
        role.setSort(0);
        return this.save(role);
    }

    @Override
    public boolean updateRole(Long id, UmsRole role) {
        UmsRole byId = this.getById(id);
        if (byId == null) {
            throw new RuntimeException("角色不存在");
        }
        role.setId(id);
        return this.updateById(role);
    }

    @Override
    public boolean delete(List<Long> ids) {
        return this.removeBatchByIds(ids);
    }

    @Override
    public Page<UmsRole> pageList(String keyword, Integer pageSize, Integer pageNum) {
        Page<UmsRole> page = Page.of(pageNum, pageSize);
        //queryWrapper组装查询where条件
        LambdaQueryWrapper<UmsRole> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(keyword)) {
            //查询条件 name 模糊
            queryWrapper.like(UmsRole::getName, keyword);
        }
        //发起查询
        this.page(page, queryWrapper);
        return page;
    }

    @Override
    public List<UmsMenu> listMenu(Long roleId) {
        return umsRoleDao.getMenuListByRoleId(roleId);
    }

    @Override
    public List<UmsResource> listResource(Long roleId) {
        return umsRoleDao.getResourceListByRoleId(roleId);
    }

    @Override
    public int allocMenu(Long roleId, List<Long> menuIds) {
        //先删除原有关系

        umsRoleMenuRelationMapper.delete(new QueryWrapper<UmsRoleMenuRelation>().eq("role_id", roleId));
        //批量插入新关系
        for (Long menuId : menuIds) {
            UmsRoleMenuRelation relation = new UmsRoleMenuRelation();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            umsRoleMenuRelationMapper.insert(relation);
        }
        return menuIds.size();
    }

    @Override
    public int allocResource(Long roleId, List<Long> resourceIds) {
        umsRoleResourceRelationMapper.delete(new QueryWrapper<UmsRoleResourceRelation>().eq("role_id", roleId));
        //批量插入新关系
        for (Long resourceId : resourceIds) {
            UmsRoleResourceRelation relation = new UmsRoleResourceRelation();
            relation.setRoleId(roleId);
            relation.setResourceId(resourceId);
            umsRoleResourceRelationMapper.insert(relation);
        }
        return resourceIds.size();
    }
}
